
import _ from 'lodash'
import { rdbmsType } from './localData'
var reg = /^['|"](.*)['|"]$/;
// use：使用
// componse：组合
// merge：合并
// 添加中文名字
export function mergeMetaCh(newSource, sources) {
    let newSource1 = _.cloneDeep(newSource)
    let sources1 = _.cloneDeep(sources)
    sources1.forEach((item) => {
        item.obj = {};
        item.parameters.map((item2) => {
            item.obj[item2.name] = item2.zhName;
        });
        newSource1.forEach((items) => {
            if (item.name == items.name) {
                items.parameters.map((item22) => {
                    item22.zhName = item.obj[item22.name];
                });
            }
        });
    });
    return newSource1
}
// metadata添加一个选中标识

export function setCheck(data) {
    let newData = _.cloneDeep(data)
    newData && newData.forEach(item => {
        item.parameters = item.parameters ? item.parameters.map(ele => {
            if (ele.defaultValue == 'true') {
                ele.defaultValue = true
            }
            if (ele.defaultValue == 'false') {
                ele.defaultValue = false
            }
            return {
                ...ele,
                isCheck: ele.optional == false ? true : false,
                // defaultValue: ele.type && ele.type[0] === 'BOOL' && ele.defaultValue == 'false' ? false : ele.defaultValue
            }
        }) : []
    })
    return newData
}
// 过滤出来 自定义属性 (判断name是否存在,不存在说明是自定义属性)
export function getCustomData(list1, list2) {
    let newlist1 = _.cloneDeep(list1)
    let newlist2 = _.cloneDeep(list2)
    const result = newlist1.filter(i1 => {
        return !newlist2.find(i2 => i1.name == i2.name)
    });
    return result
}
// 拼接最后提交的输入框组数据
export function getSourceMapList(data) {
    console.log(data, 'datadatadata')
    if (!data) {
        return []
    }
    let newData = _.cloneDeep(data)
    newData = newData.filter(item => item.isCheck)
    return newData.map(item => {
        return `${item.name} = "${item.defaultValue}"`
    })
}
// 拼接回显的数据
export function setSourceMapList(data, sourceList) {
    if (!data) {
        return {}
    }
    let newData = _.cloneDeep(data)
    let newSourceList = _.cloneDeep(sourceList)

    newData = newData.map((item) => {
        return {
            name: item.split(" = ")[0],
            defaultValue: item.split(" = ")[1].replace(reg, "$1"), //去除首位的引号
            isCheck: true
        };
    });
    return {
        options: mapListsBetween(newSourceList, newData),
        customOption: getCustomData(newData, newSourceList)
    }
}
// 回显是否是必填项
// export const mapListsBetween = list1 => list2 => {
//     const howToReduce = (maps, item) => { maps[item.name] = item.optional; return maps; };
//     const howToMap = item => ({ ...item, optional: mapOptions[item.name], });
//     const mapOptions = list1.reduce(howToReduce, {});
//     return list2.map(howToMap);
// };
// 回显是否是必填项
export const mapListsBetween = (list1, list2) => {
    let newlist2 = _.cloneDeep(list1)
    let newlist1 = _.cloneDeep(list2)
    for (let i = 0; i < newlist1.length; i++) {
        for (let j = 0; j < newlist2.length; j++) {
            if (newlist1[i].name == newlist2[j].name) {
                newlist2[j].defaultValue = newlist1[i].defaultValue || newlist2[j].defaultValue
                newlist2[j].isCheck = true
            }
        }
    }
    return newlist2
};
//  payloadOrAttribute键值形式的提交格式 (数组转对象)
export function getMapKeyValobj(data) {
    let newData = _.cloneDeep(data)
    let obj = {}
    newData.forEach(item => {
        // 数组转对象
        // [{name:x,defaultValue:1}]==>{x:1}
        obj[item.name] = item.defaultValue
    })
    return obj
}
//  payloadOrAttribute键值形式的回显格式 对象转数组
export function getMapKeyValArr(data) {
    let obj = _.cloneDeep(data)
    let arr = []
    for (let prop in obj) {
        arr.push({
            name: prop,
            defaultValue: obj[prop]
        })
    }
    return arr
}
export function valiType(val, types) {
    const newTypes = _.cloneDeep(types)
    let msgs = []
    newTypes && newTypes.some(item => {
        switch (item) {
            case 'BOOL':
                if (val === true || val === false || val === 'true' || val === 'false') {
                    msgs = []
                } else {
                    msgs.push('必须为' + item + "类型")
                }

                break;
            case 'INT':
                if (!isNaN(Number(val))) {
                    msgs = []
                } else {
                    msgs.push('必须为' + item + "类型")
                }
                break;
            case 'DOUBLE':
                if (/^[-+]?[0-9]+$/.test(val)) {
                    msgs = []
                } else {
                    msgs.push('必须为' + item + "类型")
                }
                break;
            default:
                msgs = []
                break;
        }
        return msgs
    })
    return msgs
}
const datasourceJdbcJndi = ['datasource', 'jdbc.url', 'jndi.resource']
export function setOptions(params, parameters) {
    let data = _.cloneDeep(params)
    let newParameters = _.cloneDeep(parameters)
    let id = 0
    data = data.map((item) => {
        return {
            name: item.split(" = ")[0],
            defaultValue: item.split(" = ")[1].replace(reg, "$1"), //去除首位的引号
            isCheck: true
        };
    });
    const dataName = data.find(p => {
        return datasourceJdbcJndi.includes(p.name)
    })
    let newDataPar = {}
    newParameters.parameters2.forEach(item => {
        const test = item.parameters.find(p => {
            return p.name == dataName.name
        })
        if (test) {
            newDataPar = item
        }

    })
    newDataPar.parameters = mapListsBetween(newDataPar.parameters, data)
    return newDataPar
}
export function setTypeStores(params, option) {
    // 第一个参数是必填的(回显的时候判断必填项是哪个来区分类别)
    const inlineConfig = ['jdbc.url', 'username', 'password', 'jdbc.driver.name', 'pool.properties', 'table.name', 'field.length']
    const datasource = ['datasource', 'pool.properties', 'table.name', 'field.length']
    const jndiResource = ['jndi.resource', 'pool.properties', 'table.name', 'field.length']
    const data = _.cloneDeep(params)
    let rdbms = data.find(item => item.name == 'rdbms')
    rdbms.parameters2 = rdbmsType
    if (!option) {
        return data
    }

}